<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 10:25
 */
namespace app\api\controller\v1;
use app\common\model\ShopCartGoodsModel;
use app\api\controller\v1\YanzhengController;
use app\api\controller\BaseController;
use think\Controller;

class ShopcartController extends BaseController
{
   //加入购物车  （提供 authKey 和  商品id  goods_id  用户id  用户等级 vip string）
      public function addShopCart ()
      {
          //判断请求方式
          $request = request();
          $method = $request->method();
        if (!($method == "POST"))
        {
            return json(['code'=>10004,'msg'=>'参数缺失','data'=>'']);
        }
          $param = $request->param();

          //验证器
          $param = $request->param();
          $result = $this->validate($param,'ShopCart.addShopCart');
          if(true !== $result){
              // 验证失败 输出错误信息
              return  json(['code'=>10002,'msg'=> $result,'data'=>'']);
          }

        // 鉴权

          if (!($this->check_auth($param['authKey']))){
              return json(['code'=>70001,'msg'=>'无访问权限','data'=>'']);
          }

            $model = new ShopCartGoodsModel();
            $res =  $model->addShopCart($param['goods_id'],$param["vip"],$param["userid"],$param["shop_id"]);
            if ($res)
            {
                return  json(['code'=>1,'msg'=>'添加成功','data'=>'']);
            }else
            {
                return  json(['code'=>50001,'msg'=>'添加失败','data'=>'']);
            }


      }

      //购物车列表
    public function shopCartList ()
    {
        $request = request();
        $param = $request->param();

       // 验证
        $yanzheng = new YanzhengController();
         $res =   $yanzheng->yanzheng($request,'ShopCart.shopCartList');
        if ($res)
        {
            return $res;
        }

//
        // 鉴权

        if (!( $this->check_auth($param['authKey']))){
            return json(['code'=>70001,'msg'=>'无访问权限','data'=>'']);
        }

        $model = new ShopCartGoodsModel();
        $data = $model->shopCartList($param['userid']);


        if ($data)
        {
            $data = $data->toArray();
            return  json(['code'=>1,'msg'=>'获取成功','data'=> $data]);
        }else
        {
            return  json(['code'=>50001,'msg'=>'数据为空','data'=>'[]']);
        }
    }

    //增加数量  购物车id cart_id   authKey
    public function jiaCount ()
    {
        $request = request();
        $param = $request->param();

        // 验证
        $yanzheng = new YanzhengController();
        $res =   $yanzheng->yanzheng($request,'ShopCart.jiaCount');
        if ($res)
        {
            return $res;
        }

        // 鉴权

        if (!( $this->check_auth($param['authKey']))){
            return json(['code'=>70001,'msg'=>'无访问权限','data'=>'']);
        }

        $model = new ShopCartGoodsModel();
        $res = $model->jiaCount($param['cart_id']);
        return $res;



    }

    //减少数量
    public function jianCount ()
    {
        $request = request();
        $param = $request->param();
        // 验证
        $yanzheng = new YanzhengController();
        $res =   $yanzheng->yanzheng($request,'ShopCart.jianCount');
        if ($res)
        {
            return $res;
        }

        // 鉴权

        if (!( $this->check_auth($param['authKey']))){
            return json(['code'=>70001,'msg'=>'无访问权限','data'=>'']);
        }

        $model = new ShopCartGoodsModel();
        $res = $model->jianCount($param['cart_id']);
        return $res;

    }


    //删除   [12,12,14,] authKey
    public  function cartDel ()
    {
        $request = request();
        $param = $request->param();

        // 验证
        $yanzheng = new YanzhengController();
        $res =   $yanzheng->yanzheng($request,'ShopCart.cartDel');
        if ($res)
        {
            return $res;
        }

        // 鉴权

        if (!( $this->check_auth($param['authKey']))){
            return json(['code'=>70001,'msg'=>'无访问权限','data'=>'']);
        }

        $model = new ShopCartGoodsModel();
        $res = $model->cartDel($param['cart_id']);
        return $res;

    }

    //结算   authKey cart_id user_id
    public function jieshuanCart ()
    {
        $request = request();
        $param = $request->param();

        // 验证
        $yanzheng = new YanzhengController();
        $res =   $yanzheng->yanzheng($request,'ShopCart.jieshuanCart');
        if ($res)
        {
            return $res;
        }
        // 鉴权

        if (!( $this->check_auth($param['authKey']))){
            return json(['code'=>70001,'msg'=>'无访问权限','data'=>'']);
        }
        //查询商品
        $model = new ShopCartGoodsModel();
        $data = $model->jieshuanCart($param['cart_id']);
        //查询地址 和总价
        $addressData = $model->getaddress($param['userid']);

        $allData = [ 'goods' => $data,'address'=> $addressData];
        if ($allData)
        {
            return json(['code'=>1,'msg'=>'获取成功','data'=>$allData]);
        }else
        {
            return json(['code'=>50001,'msg'=>'数据未找到','data'=>'']);
        }
    }

}